<script setup lang="ts">
import { ref } from 'vue'
import * as ElIcons from '@element-plus/icons'
import { toLine } from '../../../utils'
import { useCopy } from '../../../hooks/useCopy/index.ts'

let props = defineProps<{
  // 弹出框的标题
  title: string
  // 控制弹出框的显示与隐藏
  visible: boolean
  // 按钮类型
  type: string
}>()

let emits = defineEmits(['update:visible'])
let dialogVisible = ref<boolean>(props.visible)

let handleClick = () => {
  dialogVisible.value = !props.visible
  emits('update:visible', dialogVisible.value)
}

let handleDialogClose = () => {
  dialogVisible.value = !props.visible
  emits('update:visible', dialogVisible.value)
}

let clickItem = (item: string) => {
  let text = `<el-icon-${toLine(item)} />`
  useCopy(text)
  dialogVisible.value = false
}
</script>

<template>
  <el-button @click="handleClick" :type="type">
    <slot></slot>
  </el-button>
  <div class="m-choose-icon-dialog-body-height">
    <el-dialog
      :title="title"
      :model-value="dialogVisible"
      width="40%"
      @close="handleDialogClose"
    >
      <div class="container">
        <div
          class="item"
          v-for="(item, index) in Object.keys(ElIcons)"
          :key="index"
          @click="clickItem(item)"
        >
          <div>
            <component :is="`el-icon-${toLine(item)}`"></component>
          </div>
          <div>{{ item }}</div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.container {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}

.item {
  width: 25%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  margin-bottom: 25px;
  cursor: pointer;
  font-size: 13px;
}

svg {
  width: 2em;
  height: 2em;
}
</style>
